﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Telerik.Web.Mvc.UI;
using Cliente_DozenContext;
using System.Web.Routing;
using Telerik.Web.Mvc;
using Telerik.Web.Mvc.Extensions;
using Telerik.Web.Mvc.Configuration;
using Telerik.Web.Mvc.UI;

namespace Sistema_de_Gestion_DOZEN.Controllers
{
    public class GridAccesoClienteController : Controller
    {
        //
        // GET: /GridAccesoCliente/
        private Cliente_DozenDataContext dbCliente = new Cliente_DozenDataContext();

        public ActionResult Index(GridEditMode? mode, GridButtonType? type)
        {
            ViewData["mode"] = mode ?? GridEditMode.PopUp;
            ViewData["type"] = type ?? GridButtonType.ImageAndText;

             



            //// Se obtienen datos del modelo que serán mostrados en la vista y se agregan en el ViewBag
            //decimal ultimoCodigo = (from cli in dbCliente.Cliente_Clientes
            //                        select cli.Cliente_ClienteCod).Max();
            //ViewBag.codigo = "" + (ultimoCodigo + 1);
            //var tipoClientes = from a in dbCliente.Cliente_TipoClientes select a;
            //var planes = from plans in dbCliente.Cliente_Plans select plans;
            //List<string> lista = new List<string>();
            //lista.Add("Masculino");
            //lista.Add("Femenino");
            //ViewBag.sexos = lista;
            //ViewBag.tipoClientes = tipoClientes.ToList();
            //ViewBag.planes = planes.ToList();
            var cursosSelect = from cursos in dbCliente.Cliente_Cursos select cursos;

            ViewBag.Cursos = cursosSelect.ToList();

            //Se seleccionan todos los clientes a ser mostrados en el grid
            var clases = from a in dbCliente.Cliente_Clases where a.Cliente_ClaseFecha > DateTime.Now.AddMinutes(-1) select a;
            return View(clases);
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Insert(GridEditMode mode, GridButtonType type)
        {
            ViewData["mode"] = mode;
            ViewData["type"] = type;
            decimal ultimoCodigo = (from cli in dbCliente.Cliente_Clientes
                                    select cli.Cliente_ClienteCod).Max();
            //Create a new instance of the EditableProduct class.
            Cliente_Clase clase = new Cliente_Clase();

            clase.Cliente_ClaseFecha = DateTime.Now;

            //Perform model binding (fill the product properties and validate it).
            if (TryUpdateModel(clase))
            {
                //The model is valid - insert the product and redisplay the grid.
                dbCliente.Cliente_Clases.InsertOnSubmit(clase);
                dbCliente.SubmitChanges();
                //GridRouteValues() is an extension method which returns the 
                //route values defining the grid state - current page, sort expression, filter etc.
                RouteValueDictionary routeValues = this.GridRouteValues();
                // add the editing mode to the route values
                routeValues.Add("mode", mode);
                return RedirectToAction("Index", routeValues);
            }
            //The model is invalid - render the current view to show any validation errors
            return View("Index");
        }

    }
}
